package com.lijin.batchsys.web.service.impl;

import java.util.List;

import com.lijin.batchsys.framework.control.BatchControl;
import com.lijin.batchsys.framework.dao.BatchLogDAO;
import com.lijin.batchsys.framework.dao.model.BatchExecLog;
import com.lijin.batchsys.framework.dao.model.BatchExecLogDetail;
import com.lijin.batchsys.framework.enums.BatchPageTable;
import com.lijin.batchsys.framework.enums.ErrBatchHandleMode;
import com.lijin.batchsys.framework.enums.RunningMode;
import com.lijin.batchsys.web.bean.PageInfo;
import com.lijin.batchsys.web.service.BatchLogService;

/**
 * 
 * @author HanBowen
 *
 */
public class BatchLogServiceImpl extends BaseServiceImpl implements BatchLogService {
	
	private BatchLogDAO  batchLogDAO;
	
	@Override
	public List<BatchExecLog> listLogInfo() {
		List<BatchExecLog> logs = batchLogDAO.queryAllBatchLog();
		return logs;
	}

	@Override
	public List<BatchExecLogDetail> listLogDetail(String batcExecLogId, RunningMode batchRunningMode) {
		
		List<BatchExecLogDetail> details = batchLogDAO.queryBatchLogDetailsByExecId(batcExecLogId, batchRunningMode);
		return details;
	}

	@Override
	public String handleException(String logID, String detailID, ErrBatchHandleMode handleMode) {
		String returnIndex = "restart";
		
		// 将原有的日志信息放入批次执行上下文中
		BatchExecLog errLog = batchLogDAO.queryBatchExecLogById(logID);
		BatchExecLogDetail errDetail = new BatchExecLogDetail();
		
		//获得发生异常的详细日志Bean
		for(BatchExecLogDetail detail : errLog.getBatchExecLogDetails()){
			if(detail.getLogDetailId().equals(detailID)){
				errDetail = detail;
				break;
			}
		}
		
		RunningMode batchRunningMode = RunningMode.parseByCode(errLog.getRunningMode());
		String batchDateStr = errDetail.getBatchDate();
		int errBatchNo = errDetail.getBatchNo();
		int errJobNo   = errDetail.getJobNo();
		int errStepNo  = errDetail.getStepNo();
		
		try{
//			BatchControl.restartBatch(handleMode, batchRunningMode, errLog, batchDateStr, errBatchNo, errJobNo, errStepNo);
		}catch (Exception e) {
			returnIndex = "error";
		}
		
		return returnIndex;
	}
	
	@Override
	public <T> void getDetailPageInfo(String batcExecLogId,PageInfo<T> pageInfo) {
		
		buildPageInfo(batcExecLogId, pageInfo, BatchPageTable.BATCH_EXEC_LOG_DETAIL, batchLogDAO);
	}
	
	
	@Override
	public <T> void getLogPageInfo(PageInfo<T> pageInfo) {
		
		buildPageInfo(null, pageInfo, BatchPageTable.BATCH_EXEC_LOG, batchLogDAO);
	}
	
	public void setBatchLogDAO(BatchLogDAO batchLogDAO) {
		this.batchLogDAO = batchLogDAO;
	}




}
